package com.dorso.controller;

import com.dorso.entity.Product;
import com.dorso.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("product")
//根据id查询产品
public class ProductController {
    @Autowired
    ProductService productService;
    //根据id查询产品
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProduct(@PathVariable long id){
        Product product=productService.getProduct(id);
        if(product==null){
            return ResponseEntity.noContent().build();
        }else {
            return ResponseEntity.ok(product);
        }
    }
    @PostMapping("")
    //添加商品
    public ResponseEntity<Product> addProduct(@RequestBody Product product){
        Product result=productService.addProduct(product);
        return ResponseEntity.ok(result);
    }
    //修改商品信息
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateProduct(@PathVariable long id,@RequestBody Product product) {
        productService.updateProduct(id,product);
        return ResponseEntity.ok().build();
    }
    //删除商品
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProduct(@PathVariable long id){
        productService.deleteProduct(id);
        return ResponseEntity.ok().build();
    }
}
